﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Qcdz.Domain.TreeViews;
namespace Ue.Qcdz.UI.Controls
{
    public class Navigation : Control, INamingContainer
    {


        public Navigation()
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            Domain.TreeViews.TreeNode node = Domain.Articles.ArticleService.TagTreeView.FindNodeBy(ParentId);

            if (node != null)
            {
                int nodesCount = node.ChildNodes.Count();

                if (nodesCount <= 0)
                    return;
                if (!string.IsNullOrEmpty(Css))
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, Css);
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                TopNum = TopNum > nodesCount ? nodesCount : TopNum;
                TopNum = TopNum == 0 ? nodesCount : TopNum;

                for (int i = 0; i < TopNum; i++)
                {
                    RenderItem(writer, node.ChildNodes.ElementAt(i));
                }
                writer.RenderEndTag();
            }
        }


        private void RenderItem(HtmlTextWriter writer, TreeNode node)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            switch (node.Key.ToString())
            {
                case ("be01f660-dd6e-4734-a8b6-9b0ec505ba37"):
                case ("577d7cc7-09fb-44b0-a251-9781b60f9740"):
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, Ue.Qcdz.Domain.SiteUrls.Instance.NewsListBigImg(node.Key));
                    break;
                case ("a67fa500-9cde-4c99-a342-edfed85d1260"):
                case ("1f990043-785b-4b4d-90cc-cb0794da326d"):
                case ("0ddbc53a-bac4-42cc-8799-8f8d958b0d74"): //车载电子
                case ("ce645533-9bed-472e-9edf-bd155e1a8808"): //汽车照明
                case ("6494ebbe-7d67-41ab-874d-0abf5d79b338"): //传感视觉
                case ("88415638-2f51-47dc-b750-3ea8ccdd64b8"): //智能防盗
                case ("0ce1e189-c218-4209-9589-631d281b64a6"): //控制系统
                case ("8d10b74a-cb29-4865-89fa-33864fd77f9b"): //汽车影音
                case ("0f456343-849a-41c8-91a3-1ceb1424b9c5"): //汽车导航
                case ("c8dcda02-7e0d-4b1f-b52f-1928987d3706"): //安全电子
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, Ue.Qcdz.Domain.SiteUrls.Instance.NewsList(node.Key));
                    break;
                case ("1d7d02d7-bc71-4f09-baa5-ae54439cad61"):
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "/user.aspx?id=7939862d-b7ae-45eb-81e8-b49b330e97b5");
                    break;
                default:
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, Ue.Qcdz.Domain.SiteUrls.Instance.NewsListSmallImg(node.Key));
                    break;
            }
            
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(node.Tag.Name);
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        #region ：Attribute
        public string Css
        {
            set;
            get;
        }
        public string ParentId
        {
            set;
            get;
        }
        public int TopNum
        {
            set;
            get;
        }
        #endregion
    }
}
